package com.example.plantplacessummer2013;

import android.content.Intent;
import android.graphics.Bitmap;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class LocationActivity extends PlantPlacesActivity {

	private static final int CAMERA_RESULT = 2;
	private static final int SELECT_PLANT_RESULT = 1;
	private ImageView imgPhoto;
	private TextView lblPlantSelectedName;
	private LocationManager locationManager;
	private LocationListener locationListener;
	private TextView lblLatValue;
	private TextView lblLngValue;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gpsplant);
		
		// get a reference to the image.
		imgPhoto = (ImageView) findViewById(R.id.imgPhoto);
		// where we display plant results.
		lblPlantSelectedName = (TextView) findViewById(R.id.lblPlantSelectedName);
		
		// get the labels that should show lat and lng on the screen.
		lblLatValue = (TextView) findViewById(R.id.lblLatValue);
		lblLngValue = (TextView) findViewById(R.id.lblLngValue);
		
		// get a location manager from the operating system.
		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		
		locationListener = new LocationListener() {

			@Override
			public void onLocationChanged(Location loc) {
				// update our lat and lng values.
				lblLatValue.setText(Double.toString(loc.getLatitude()));
				lblLngValue.setText(Double.toString(loc.getLongitude()));
				
			}

			@Override
			public void onProviderDisabled(String arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onProviderEnabled(String arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
				// TODO Auto-generated method stub
				
			}
			
		};
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60 *1000, 0, locationListener);
	}
	
	/**
	 * Handle plant selection.
	 * 
	 * This method is invoked directly via the onClick property in the layout.
	 * 
	 * @param view
	 */
	public void selectPlant(View view) {
		Intent selectPlantIntent = new Intent(this, AdvancedPlantSearch.class);
		// start this activity, and request a result in return.
		selectPlantIntent.putExtra(RESULT, true);
		startActivityForResult(selectPlantIntent, SELECT_PLANT_RESULT);
	}
	
	/**
	 * Handle photo.
	 * 
	 * This method is invoked directly via the onClick property in the layout.
	 * @param view
	 */
	public void takePhoto(View view) {
		// pick a camera provider.  As an implicit intent, we don't care which one.
		Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(cameraIntent, CAMERA_RESULT);
		
	}
	
	/**
	 * Handle returns from onActivityResult.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// make sure we received a successful result.
		if (resultCode ==  RESULT_OK) {
		
			// handle a photo.
			if (requestCode == CAMERA_RESULT) {
				// get data back from the camera
				Bitmap image = (Bitmap) data.getExtras().get("data");
				
				// display that data on the image view.
				imgPhoto.setImageBitmap(image);
			} else if (requestCode == SELECT_PLANT_RESULT) {
				// get the data out from the advanced search screen.
				String plantResult = data.getStringExtra(PLANT_RESULT);
				// put this in the label for the plant result.
				lblPlantSelectedName.setText(plantResult);
			}
		}
	}
	
}
